package com.example.arm.common.lifecycle

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner

/**
 *  author : yanghaozhang
 *  date : 2022/12/27 16:33
 *  description :
 */
sealed class BaseLifecycleBlock {

    fun transformEvent(): Lifecycle.Event {
        return when (this) {
            is CreateBlock, is CreateWithOwnerBlock -> Lifecycle.Event.ON_CREATE
            is StartBlock, is StartWithOwnerBlock -> Lifecycle.Event.ON_START
            is ResumeBlock, is ResumeWithOwnerBlock -> Lifecycle.Event.ON_RESUME
            is PauseBlock, is PauseWithOwnerBlock -> Lifecycle.Event.ON_PAUSE
            is StopBlock, is StopWithOwnerBlock -> Lifecycle.Event.ON_STOP
            is DestroyBlock, is DestroyWithOwnerBlock -> Lifecycle.Event.ON_DESTROY
        }
    }

    fun invokeBlock(owner: LifecycleOwner) {
        when (this) {
            is CreateBlock -> block.invoke()
            is StartBlock -> block.invoke()
            is ResumeBlock -> block.invoke()
            is PauseBlock -> block.invoke()
            is StopBlock -> block.invoke()
            is DestroyBlock -> block.invoke()

            is CreateWithOwnerBlock -> block.invoke(owner)
            is StartWithOwnerBlock -> block.invoke(owner)
            is ResumeWithOwnerBlock -> block.invoke(owner)
            is PauseWithOwnerBlock -> block.invoke(owner)
            is StopWithOwnerBlock -> block.invoke(owner)
            is DestroyWithOwnerBlock -> block.invoke(owner)
        }
    }
}

class CreateBlock(val block: LifecycleCallBack) : BaseLifecycleBlock()
class StartBlock(val block: LifecycleCallBack) : BaseLifecycleBlock()
class ResumeBlock(val block: LifecycleCallBack) : BaseLifecycleBlock()
class PauseBlock(val block: LifecycleCallBack) : BaseLifecycleBlock()
class StopBlock(val block: LifecycleCallBack) : BaseLifecycleBlock()
class DestroyBlock(val block: LifecycleCallBack) : BaseLifecycleBlock()

class CreateWithOwnerBlock(val block: LifecycleWithOwnerCallBack) : BaseLifecycleBlock()
class StartWithOwnerBlock(val block: LifecycleWithOwnerCallBack) : BaseLifecycleBlock()
class ResumeWithOwnerBlock(val block: LifecycleWithOwnerCallBack) : BaseLifecycleBlock()
class PauseWithOwnerBlock(val block: LifecycleWithOwnerCallBack) : BaseLifecycleBlock()
class StopWithOwnerBlock(val block: LifecycleWithOwnerCallBack) : BaseLifecycleBlock()
class DestroyWithOwnerBlock(val block: LifecycleWithOwnerCallBack) : BaseLifecycleBlock()